﻿using JESAI.Application.Abstractions.Admin;
using JESAI.Application.Abstractions.Admin.Dtos.Platform;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Core.Attributes;
using JESAI.Platform.Domain.Admin.Entities;
using JESAI.Platform.Domain.Admin.Repositories;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.Admin
{
    [AppService]
    public class PlatformService : IPlatformService
    {
        private readonly IPlatformRepository _platformRepository;

        public PlatformService(IPlatformRepository platformRepository)
        {
            _platformRepository = platformRepository;
        }

        public void DeletePlatform(long id)
        {
            _platformRepository.DeleteById(id);
        }

        public void DeletePlatforms(long[] ids)
        {
            _platformRepository.DeleteByIds(ids as dynamic);
        }

        public PlatformDto QueryListByPlatformKey(string platformKey)
        {
            var model = _platformRepository._DbQueryable.First(it => it.Key == platformKey && !it.IsDel);
            if (model != null)
                return model.MapTo<PlatformDto>();

            return null;
        }

        public List<PlatformDto> QueryPlatformListByIds(int[] ids)
        {
            var modelList = _platformRepository._DbQueryable.Where(it => it.IsDel == false && ids.Any(o => o == it.Id)).ToList();

            return modelList.MapTo<List<PlatformDto>>();
        }

        public List<PlatformDto> QueryPlatforms()
        {
            var modelList = _platformRepository._DbQueryable.OrderBy(it => it.SortId, OrderByType.Asc).ToList();

            return modelList.MapTo<List<PlatformDto>>();
        }

        public void UpdateOrInsert(SetPlatformInput input)
        {
            var model = input.MapTo<PlatformModel>();
            if (model.Id > 0)
            {
                _platformRepository.AsUpdateable(model).ExecuteCommand();
            }
            else
            {
                model.AddTime = DateTime.Now;
                model.IsDel = false;
                _platformRepository.AsInsertable(model).ExecuteCommand();
            }
        }
    }
}
